package com.app.oasystem.service.impl;


import com.app.oasystem.dto.PageGridDto;
import com.app.oasystem.entity.Leaves;
import com.app.oasystem.entity.Record;
import com.app.oasystem.entity.User;
import com.app.oasystem.exception.CommonException;
import com.app.oasystem.mapper.LeavesMapper;
import com.app.oasystem.mapper.UserMapper;
import com.app.oasystem.service.LeavesService;
import com.app.oasystem.utils.DateUtils;
import com.app.oasystem.utils.TableDtoUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 86+
 * @data 2021-04-13
 */


@Service
public class LeavesServiceImpl extends ServiceImpl<LeavesMapper, Leaves> implements LeavesService {
    @Resource
    private LeavesMapper leavesMapper;

    @Resource
    private UserMapper userMapper;

    /**
     * 注意当前的id主键 是默认的"id"，具体情况根据数据库主键的名称进行修改
     */
    @Override
    public Leaves getLeavesInfoById(Integer id) {
        Leaves leaves = null;
        try {
            leaves = leavesMapper.selectOne(new QueryWrapper<Leaves>().eq("id", id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (null == leaves) {
            throw new CommonException("当前查询的:[{leaves}]不存在");
        }
        return leaves;
    }

    /**
     * 注意当前的id主键 是默认的"id"，具体情况根据数据库主键的名称进行修改
     */
    @Override
    public boolean approval(Integer id, Integer leaveId) {
        int row = 0;
        try {
            row = leavesMapper.update(new Leaves().setIsAgreed(leaveId), new UpdateWrapper<Leaves>().eq("id", id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return row > 0;
    }

    /**
     * 注意当前的name 名称 是默认的"name"，具体情况根据数据库对应的名称进行修改
     */
    @Override
    public Leaves getLeavesInfoByName(String leavesName) {
        Leaves leaves = null;
        try {
            leaves = leavesMapper.selectOne(new QueryWrapper<Leaves>().eq("name", leavesName));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("[{leaves}] 未知异常");
        }
        if (null == leaves) {
            throw new CommonException("当前查询的:[{leaves}]不存在");
        }
        return leaves;
    }

    @Override
    public boolean updateLeaves(Leaves leaves) {
        int id = 0;
        try {
            id = leavesMapper.update(leaves, new UpdateWrapper<Leaves>().eq("id", leaves.getId()));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("[{leaves}] 未知异常");
        }
        return id > 0;
    }

    @Override
    public boolean insertLeaves(Leaves leaves) {
        int id = 0;
        try {
            User user = userMapper.selectOne(new QueryWrapper<User>().eq("id", leaves.getUserId()));
            leaves.setIsAgreed(0);
            leaves.setApplyName(user.getUsername());
            leaves.setDays(DateUtils.getBetweenStartAndEndDay(leaves.getEndTime(),leaves.getBeginTime()));
            id = leavesMapper.insert(leaves);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("[{leaves}] 未知异常");
        }
        return id > 0;
    }

    @Override
    public boolean deleteLeaves(Leaves leaves) {
        int id = 0;
        try {
            id = leavesMapper.deleteById(leaves.getId());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("[{leaves}] 未知异常");
        }
        return id > 0;
    }

    @Override
    public Object getLeavesList(PageGridDto pageGridDto) {
        Page<Leaves> page = new Page<>(pageGridDto.getPage(), pageGridDto.getlimit());
        Page leavesPage = null;
        Object search = pageGridDto.getSearchCondition().get("search");
        if (pageGridDto.getSearchCondition().size() != 0) {
            leavesPage = leavesMapper.selectPage(page, new QueryWrapper<Leaves>().like("applyName", search).orderByDesc("beginTime"));
            System.out.println("Leaves List : " + leavesPage.getRecords());
        } else {
            leavesPage = leavesMapper.selectPage(page, new QueryWrapper<Leaves>().orderByDesc("beginTime"));
        }
        return TableDtoUtils.setTableDto(leavesPage.getTotal(), leavesPage.getRecords());
    }
}
